/**
 * 本类主要是后台管理员使用的。
 *
 */
package com.webshop.DataDao;

import com.webshop.JavaBean.Category;
import com.webshop.JavaBean.Goods;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class GoodsAdmain {

    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    /**
     * 管理员
     * 上架商品
     * @param product
     * @throws Exception
     */
    public void insertProducts(Goods product) throws Exception {

        String sql = "insert into goods (productName,isHot,owner,setAttribute,mainPic,state,createTime,category) values(?,?,?,?,?,?,?,?)";
        Object[] parm = {product.getProductName(), product.getIsHot(), product.getOwner(),
                product.getSetAttribute(), product.getMainPic(), product.getState(), product.getCreateTime(), product.getCategory()};
        qr.update(sql, parm);

    }

    /**
     * 管理员
     * 下架商品或恢复上架商品
     * @param ID
     * @throws Exception
     */
    public void offProducts(String ID) throws Exception {
        String sql = "update goods set state = 0 where ID =?";
        qr.update(sql,  ID);
    }

    /**
     * 恢复上架商品
     * @param ID
     * @throws Exception
     */
    public void upProduct(String ID) throws Exception {
        String sql = "update goods set state = 1 where ID =?";
        qr.update(sql,  ID);
    }

    /**
     * 分页查询管理员下的商品，返回商品列表
     * @param name
     * @return
     * @throws Exception
     */
    public List<Goods> find(String name,int page,int num)throws Exception{
        String sql="select * from goods where owner = ?" +" limit ? ,?";
        List<Goods> goods=new ArrayList<>();
        int newpage=(page-1)*num;
        goods=qr.query(sql,new BeanListHandler<>(Goods.class),name,newpage,num);
        return goods;
    }

    /**
     * 返回分类
     * @param pid
     * @return
     * @throws Exception
     */
    public List<Category> showcat(int pid)throws Exception{
        String sql="select * from category where pid = ?";
        List<Category> categories=new ArrayList<>();
        categories=qr.query(sql,new BeanListHandler<>(Category.class),pid);
        return categories;
    }
}
